﻿using System;
using System.Collections.Generic;
using System.Text;
using DSAManager.Models;
using DSAManager.Models.Input;
using DSAManager.Models.Output;
using DSAManager.Utils;
using Senparc.Weixin.Entities;
using Senparc.Weixin.MP.AdvancedAPIs.QrCode;

namespace DSAManager.Core
{
    public class Api
    {
        private static string host = "http://api.mzc9.com/api/manager";
        private static string weixinhost = "http://api.mzc9.com/api/weixin";
        //private static string host = "http://localhost:11335/api/manager";
        //private static string weixinhost = "http://localhost:11335/api/weixin";

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult Login(UserLoginInfo para)
        {
            return post<InvokeResult>("Login", para);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult ChangePassword(ChgPwdInfo para)
        {
            return post<InvokeResult>("ChangePassword", para);
        }

        /// <summary>
        /// 保存用户信息
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult SaveUser(UserInfo para)
        {
            return post<InvokeResult>("SaveUser", para);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public static InvokeResult<List<UserInfo>> QueryUser()
        {
            return post<InvokeResult<List<UserInfo>>>("QueryUser", "");
        }

        /// <summary>
        /// 查询配置
        /// </summary>
        /// <returns></returns>
        public static InvokeResult<List<ConfigItem>> LoadConfigs()
        {
            return post<InvokeResult<List<ConfigItem>>>("LoadConfigs", "");
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult SaveConfigs(List<ConfigItem> para)
        {
            return post<InvokeResult>("SaveConfigs", para);
        }

        /// <summary>
        /// 保存照片尺寸
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult SavePhotosize(PhotoSizeSubmitInfo para)
        {
            return post<InvokeResult>("SavePhotosize", para);
        }

        /// <summary>
        /// 新增订单
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static InvokeResult AddOrder(DSAManager.Models.Input.TaobaoOrderInfo para)
        {
            return post<InvokeResult>("AddOrder", para);
        }

        /// <summary>
        /// 获取照片尺寸列表
        /// </summary>
        /// <returns></returns>
        public static InvokeResult<List<PhotoSizeInfo>> QueryPhotosize()
        {
            return post<InvokeResult<List<PhotoSizeInfo>>>("QueryPhotosize", "");
        }

        /// <summary>
        /// 获取未下载的订单
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public static InvokeResult<PageEntity<OrderInfo>> QueryUnDownOrders(PageRequest page)
        {
            return post<InvokeResult<PageEntity<OrderInfo>>>("QueryUnDownOrders", page);
        }


        public static InvokeResult<PageEntity<OrderInfo>> QueryHasDownOrders(PageRequest page)
        {
            return post<InvokeResult<PageEntity<OrderInfo>>>("QueryHasDownOrders", page);
        }

        public static InvokeResult SetOrderHasDown(List<string> para)
        {
            return post<InvokeResult>("SetOrderHasDown", para);
        }

        public static InvokeResult SetOrderHasSend(List<string> para)
        {
            return post<InvokeResult>("SetOrderHasSend", para);
        }

        public static InvokeResult<CreateQrCodeResult> CreateQrCode(QrCodeParaInfo para)
        {
            return post<InvokeResult<CreateQrCodeResult>>(weixinhost, "CreateQrCode", para);
        }

        public static InvokeResult<CreateQrCodeResult> CreateByStr(string para)
        {
            return post<InvokeResult<CreateQrCodeResult>>(weixinhost, "CreateByStr", para);
        }

        public static InvokeResult<WxJsonResult> CreateWeixinMenu()
        {
            return post<InvokeResult<WxJsonResult>>(weixinhost, "CreateWeixinMenu", "");
        }

        public static InvokeResult<List<SalesmanInfo>> QuerySalesmanInfo()
        {
            return post<InvokeResult<List<SalesmanInfo>>>("QuerySalesmanInfo", "");
        }

        public static InvokeResult CheckSalesmanExist(SalesmanInfo para)
        {
            return post<InvokeResult>("CheckSalesmanExist", para);
        }

        public static InvokeResult UpdateSalesmanInfo(SalesmanInfo para)
        {
            return post<InvokeResult>("UpdateSalesmanInfo", para);
        }

        public static InvokeResult InsertSalesmanInfo(SalesmanInfo para)
        {
            return post<InvokeResult>("InsertSalesmanInfo", para);
        }

        public static InvokeResult<PageEntity<RecommendInfo>> QueryRecommendInfo(PageRequest page)
        {
            return post<InvokeResult<PageEntity<RecommendInfo>>>("QueryRecommendInfo", page);
        }


        public static InvokeResult<PageEntity<UnSubscribeInfo>> QueryUnSubscribeInfo(PageRequest page)
        {
            return post<InvokeResult<PageEntity<UnSubscribeInfo>>>("QueryUnSubscribeInfo", page);
        }

        public static InvokeResult<WeidianTokenInfo> GetWeidianToken(WeidianTokenParaInfo wdtpi)
        {
            InvokeResult<WeidianTokenInfo> result = new InvokeResult<WeidianTokenInfo>();
            HttpHelper httpHelper = new HttpHelper();
            string url = "https://api.vdian.com/token?grant_type=client_credential&appkey=" + wdtpi.AppId + "&secret=" + wdtpi.AppSecret;
            string html = httpHelper.GetHtml(url);
            if (string.IsNullOrEmpty(html))
            {
                result.msg = "连接服务器失败.";
                result.ok = false;
            }
            else
            {
                result.data = JsonHelper.DeserializeObject<WeidianTokenInfo>(html);
                result.ok = true;
            }
            return result;
        }


        /// <summary>
        /// 与服务器交互方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="para"></param>
        /// <returns></returns>
        static T post<T>(string cmd, object para) where T : InvokeResult, new()
        {

            T result = default(T);
            string url = string.Format("{0}/{1}", host, cmd);
            HttpHelper httpHelper = new HttpHelper();
            string postdata = "data=" + CharSetHelper.UrlEncode(JsonHelper.SerializeObject(para), Encoding.UTF8);
            if (AppContext.LoginInfo != null)
            {
                postdata += "&u=" + AppContext.LoginInfo.user;
                postdata += "&p=" + AppContext.LoginInfo.password;
            }
            string html = httpHelper.GetHtml(url, postdata, true);
            if (string.IsNullOrEmpty(html))
            {
                result = new T();
                result.msg = "连接服务器失败.";
            }
            else
            {
                try
                {
                    result = JsonHelper.DeserializeObject<T>(html);
                }
                catch (Exception ex)
                {

                    result = new T();
                    result.msg = "服务器返回的数据无法识别!";
                }
            }

            return result;
        }

        static T post<T>(string serviceurl, string cmd, object para) where T : InvokeResult, new()
        {

            T result = default(T);
            string url = string.Format("{0}/{1}", serviceurl, cmd);
            HttpHelper httpHelper = new HttpHelper();
            string postdata = "data=" + JsonHelper.SerializeObject(para);
            string html = httpHelper.GetHtml(url, postdata, true);
            if (string.IsNullOrEmpty(html))
            {
                result = new T();
                result.msg = "连接服务器失败.";
            }
            else
            {
                try
                {
                    result = JsonHelper.DeserializeObject<T>(html);
                }
                catch (Exception ex)
                {

                    result = new T();
                    result.msg = "服务器返回的数据无法识别!";
                }
            }

            return result;
        }
    }
}
